<?php

class CoursesController extends Zend_Controller_Action 
{
    protected $_model;
	
	function indexAction()
	{
		$model = $this->_getModel();
		$view_list = $this->_model;
		$rowset = $view_list->fetchAll();

		$rowCount = count($rowset);
		$this->view->view_list = $rowset;
		$this->view->rowCount  = $rowCount;
	}
	
	function addAction()
	{
		$form = $this->_getCoursesForm();
		
        $form->submit->setLabel('Add');
        $this->view->form = $form;

		$request = $this->getRequest();
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
            	
				$model = $this->_getModel();
				$add = $this->_model;
				
				$formData = $form->getValues();
                
				$row 			  	 = $add->createRow();
				
				$date = $form->getValue('ano').'-'.$form->getValue('mes').'-'.$form->getValue('dia');
				
				$row->cod_organizador  = $form->getValue('cod_organizador');
				$row->ano  	           = $form->getValue('time');
				$row->ordem            = $form->getValue('ordem');
				$row->desc_concurso    = $form->getValue('desc_concurso');
				$row->edital_publicado = $form->getValue('edital_publicado');
				$row->sal_menor        = $form->getValue('sal_menor');
				$row->sal_maior        = $form->getValue('sal_maior');
				$row->dt_realizacao    = $date;
				$row->concluido        = $form->getValue('concluido');				
                $row->save();
                
                return $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        }
	}

	function showAction()
    {
		$cod_concurso = (int)$this->_request->getParam('cod_concurso');
		$model = $this->_getModel();
		$show = $this->_model;
		$query = $show->select()
							->setIntegrityCheck(false)
							->from($show)
							->join('tb_organizador', 'tb_organizador.cod_organizador = tb_concurso.cod_organizador')
							->where('cod_concurso='.$cod_concurso);
		$joinedRowset = $show->fetchRow($query);
		$this->view->show = $joinedRowset;
	}

	function editAction()
    {
        $form = $this->_getCoursesForm();
		
        $form->submit->setLabel('Save');
        $this->view->form = $form;
        
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                
				$model = $this->_getModel();
				$edit = $this->_model;
                
				$cod_concurso = (int)$form->getValue('cod_concurso');
                $row 	      = $edit->fetchRow('cod_concurso='.$cod_concurso);
				
				$date = $form->getValue('ano').'-'.$form->getValue('mes').'-'.$form->getValue('dia');
				
				$row->cod_organizador  = $form->getValue('cod_organizador');
				$row->ano  	           = $form->getValue('time');
				$row->ordem            = $form->getValue('ordem');
				$row->desc_concurso    = $form->getValue('desc_concurso');
				$row->edital_publicado = $form->getValue('edital_publicado');
				$row->sal_menor        = $form->getValue('sal_menor');
				$row->sal_maior        = $form->getValue('sal_maior');
				$row->dt_realizacao    = $date;
				$row->concluido        = $form->getValue('concluido');
				
                $row->save();
                
                return $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $cod_concurso = (int)$this->_request->getParam('cod_concurso', 0);
            if ($cod_concurso > 0) {
                
				$model = $this->_getModel();
				$edit = $this->_model;
				
				$edit = $edit->fetchRow('cod_concurso='.$cod_concurso);
                $form->populate($edit->toArray());
            }
        }
    }

	function deleteAction()
    {        
        if ($this->_request->isPost()) {
            $cod_concurso = (int)$this->_request->getPost('cod_concurso');
            $del = $this->_request->getPost('del');
            if ($del == 'Yes' && $cod_concurso > 0) {
                
				$model = $this->_getModel();
				$remove = $this->_model;
				
                $where = 'cod_concurso = ' . $cod_concurso;
                $remove->delete($where);
            }
            return $this->_helper->redirector('index');
        } else {
            $cod_concurso = (int)$this->_request->getParam('cod_concurso');
            if ($cod_concurso > 0) {
                
				$model = $this->_getModel();
				$remove = $this->_model;
				
                $this->view->remove = $remove->fetchRow('cod_concurso='.$cod_concurso);
            }
        }
    }

    protected function _getModel()
    {
        if (null === $this->_model) {
            require_once APPLICATION_PATH . '/models/Courses.php';
            $this->_model = new Model_Courses();
        }
        return $this->_model;
    }
	
	protected function _getCoursesForm()
    {
        require_once APPLICATION_PATH . '/forms/CoursesForm.php';
        $form = new Form_Courses();
        return $form;
    }

}

